package com.weirong.smartvault.backservice.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.weirong.smartvault.backservice.util.chart.*;
import com.weirong.smartvault.common.tools.constant.DepartmentType;
import com.weirong.smartvault.backservice.util.table.AuthorityTree;
import com.weirong.smartvault.backservice.util.table.Tree;
import com.weirong.smartvault.backservice.util.common.DateUtil;
import com.weirong.smartvault.backservice.util.common.ListTool;
import com.weirong.smartvault.dao.*;
import com.weirong.smartvault.entity.*;
import com.weirong.smartvault.backservice.entity_model.AuthorityModel;
import com.weirong.smartvault.backservice.entity_model.RoleModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.*;

/**
 * 于2017/12/2由 KFEB4 创建 .
 */
@Controller
@RequestMapping(value = "/ajax")
public class Ajax {
    @Autowired
    private AreaRepository areaRepository;
    @Autowired
    private DepartmentRepository departmentRepository;

    @Autowired
    private DictRepository dictRepository;

    @Autowired
    private RoleRepository roleRepository;
    Gson gson = new Gson();

    @RequestMapping(value = "/getCity", method = RequestMethod.GET)
    @ResponseBody
    public String getCity(@RequestParam(value = "areaId", defaultValue = "-1") Integer areaId) {
        if (areaId < 0)
            return "[]";
        Area area = areaRepository.getOne(areaId);
        List<Area> sons = areaRepository.findAllByParentCode(area.getCode());
        return gson.toJson(sons);
    }

    @RequestMapping(value = "/getSuperiorDepartment", method = RequestMethod.GET)
    @ResponseBody
    public String getSuperiorDepartment(@RequestParam(value = "category", defaultValue = DepartmentType.fh) String category) {
        String superior;
        if (Objects.equals(category, DepartmentType.fh)) {
            return "[]";
        } else if (Objects.equals(category, DepartmentType.zh)) {
            superior = DepartmentType.fh;
        } else
            superior = DepartmentType.zh;
        Dict dict = dictRepository.findByName(superior);
        List<Department> departments = departmentRepository.findAllByType(dict.getCode());
        return gson.toJson(departments);
    }

    @RequestMapping("/stock")
    @ResponseBody
    public String stock(@RequestParam(value = "date", defaultValue = "") String date) throws ParseException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        if (date.length() == 0) {
            date = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        }
        //按币种，面额划分
        ChartOption option = ChartStock.buildOption(date);
        //每月选取一个点
        return gson.toJson(option);
    }

    @RequestMapping("/stock_detail")
    @ResponseBody
    public String stockDetail(@RequestParam(value = "date", defaultValue = "") String date) throws InvocationTargetException, NoSuchMethodException, ParseException, IllegalAccessException {
        if (date.length() == 0) {
            date = DateUtil.getThisMonth();
        }
        ChartOption option = ChartStockDetail.buildOption(date);
        return gson.toJson(option);
    }

    @RequestMapping("/use")
    @ResponseBody
    public String use(@RequestParam(value = "date", defaultValue = "") String date) throws InvocationTargetException, NoSuchMethodException, ParseException, IllegalAccessException {
        if (date.length() == 0) {
            date = DateUtil.getThisYear();
        }
        ChartOption option = ChartUse.buildOption(date);
        return gson.toJson(option);
    }

    @RequestMapping("/use_detail")
    @ResponseBody
    public String useDetail(@RequestParam(value = "date", defaultValue = "") String date) throws InvocationTargetException, NoSuchMethodException, ParseException, IllegalAccessException {
        if (date.length() == 0) {
            date = DateUtil.getThisMonth();
        }
        ChartOption option = ChartUseDetail.buildOption(date);
        return gson.toJson(option);
    }

    @RequestMapping(value = "compare_stock_use")
    @ResponseBody
    public String compareStockUse(@RequestParam(value = "date", defaultValue = "") String date) throws InvocationTargetException, NoSuchMethodException, ParseException, IllegalAccessException {
        if (date.length() == 0) {
            date = DateUtil.getThisYear();
        }
        ChartOption option = ChartCompareStockUse.buildOption(date);
        return gson.toJson(option);
    }

    @RequestMapping(value ="get_all_permission" )
    @ResponseBody
    public String getAllPermission() throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //返回一颗完整的权限树
        List<Tree<AuthorityModel>> roots = AuthorityTree.getAllInTree();
        Gson gson = new GsonBuilder()
                .excludeFieldsWithoutExposeAnnotation()
                .create();
        return gson.toJson(roots);
    }


    @RequestMapping(value = "get_role_permission")
    @ResponseBody
    public String getRolePermission(@RequestParam(value = "id", defaultValue = "-1") Integer id) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        Role role = roleRepository.findOne(id);
        RoleModel roleModel = new RoleModel(role);
        List ids = ListTool.map(roleModel.getAllAuthority(),AuthorityModel::getId);
        return gson.toJson(ids);
    }
}
